//
//  FeedBackViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/12.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "FeedBackViewController.h"

NSString const *FeedBackViewControllerTitleParamKey = @"FeedBackViewControllerTitleParamKey";

@interface FeedBackViewController ()<XUIGroupTableViewDelegate,XUIGroupTableViewDataSource,UITextViewDelegate,XUIButtonDelegate>
{
    XUIGroupTableView *_gtable;
    
    XUITextView *_textView;
    
    RSWDataOperation *_manager;
}
@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    NSString *title = self.param[FeedBackViewControllerTitleParamKey];
    
    [self setBackNavigateItemTitle:title rightItemText:@"提交"];
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUIShinColorTextStyle2196F3Color];
    
    [self initVariable];
    [self initUI];
}

- (void)initVariable
{
    _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
}

- (void)initUI
{
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    
    _gtable = [[XUIGroupTableView alloc]initWithFrame:CGRectMake(0, top, self.view.width, self.view.height)];
    _gtable.backgroundColor = [[UITableView appearance] backgroundColor];
    _gtable.delegate = self;
    _gtable.dataSource = self;
    
    [self.view addSubview:_gtable];
}

-(void)viewControllerDidMoveDealloc
{
    [super viewControllerDidMoveDealloc];
}

#pragma mark --
#pragma mark -- groupTableView delegate

- (NSUInteger)numberOfGroupsInGroupTableView:(XUIGroupTableView *)groupTableView
{
    return 1;
}

- (NSUInteger)groupTableView:(XUIGroupTableView *)groupTableView numberOfRowsInGroup:(NSInteger)group
{
    return 1;
}

-(CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForHeaderInGroup:(NSInteger)group
{
    return 0;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
        return self.view.height - 44 - 20;
    return 44;
}

- (XUIGroupTableViewCell *)groupTableView:(XUIGroupTableView *)groupTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XUIGroupTableViewCell *cell = [[XUIGroupTableViewCell alloc] initWithFrame:CGRectZero];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat height = [self groupTableView:groupTableView heightForRowAtIndexPath:indexPath];
    
    switch(indexPath.section )
    {
        case 0:
        {
            if(_textView == nil)
            {
                _textView = [[XUITextView alloc]initWithFrame:CGRectMake(0, 5, groupTableView.width, height - 10)];
                _textView.delegate = self;
                _textView.font = [UIFont systemFontOfSize:16];
                _textView.placeHolder = @"请输入您的宝贵意见";
                _textView.textInputMaxLength = 400;
                _textView.hiddenDoneButton = NO;
            }
            [cell.contentView addSubview:_textView];
            
            break;
        }
    }
    
    return cell;
}

#pragma mark --
#pragma makr -- submit

-(void)beginProgressView:(XMSDataOperationManager *)dom
{
    [super beginProgressViewPanel:@"提交中，请稍候"];
}

-(void)clickedRightBarButton:(id)sender
{
    if((_textView.text.length <= 0 || [_textView.text isBlankString]))
    {
        [UIAlertView showInfoMsg:@"提交内容不能为空"];
        return;
    }
    
    [UIViewController closeKeyBoard];

    [_manager userFeedBackWithFeedContent:_textView.text completion:^(NSInteger nResult, BOOL bResult, NSError *error)
     {
         [self filterRequestResult:nResult defaultError:error noFilter:^(NSInteger requestResult)
          {
              switch(requestResult)
              {
                  case YES:
                  {
                      if(bResult == YES)
                      {
                          [XUIAlertView alertViewWithTitle:@"提示" message:@"反馈成功" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex)
                           {
                               [XUINavigationController pop];
                           }];
                      }
                      else
                          [UIAlertView showErrorMsg:@"反馈失败"];
                      break;
                  }
                  default:
                  {
                      [UIAlertView showErrorMsg:@"反馈失败"];
                      break;
                  }
              }
          }];
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
